package com.jf.viewadapter;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jf.assistant.JfLog;
import com.jf.assistant.R;
import com.jf.assistant.SettingActivity;
import com.jf.assistant.databinding.LyActionlistBinding;
import com.jf.assistant.databinding.LyActiontasklistBinding;
import com.jf.bean.Action;
import com.jf.bean.AssistantTask;
import com.kongzue.dialog.v2.CustomDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

// ① 创建Adapter
public  class ActionListAdapter extends RecyclerView.Adapter<ActionListAdapter.VH>{
    //② 创建ViewHolder
    public static class VH extends RecyclerView.ViewHolder{
        public final TextView actionname;
        public final TextView tvAction;
        public Button btdele;
        public Button etedit;
        public VH(LyActionlistBinding v) {
            super(v.getRoot());
            actionname = v.tvActionname;
            tvAction=v.tvAction;
            btdele=v.btDele;
            etedit=v.btEdit;
        }
    }

    private ArrayList<Action> mDatas;
    private  Activity mActivity;
    public ActionListAdapter(ArrayList<Action> data, Activity mactivity) {
        this.mDatas = data;
        this.mActivity=mactivity;
    }

    //③ 在Adapter中实现3个方法

    @Override
    public void onBindViewHolder(ActionListAdapter.VH holder, final int position) {
        Log.e("jiafan",mDatas.get(position).getType()+"");
        switch (mDatas.get(position).getType()){
            case Action.ACTIONNAME:
                holder.actionname.setText(mDatas.get(position).actionname);
                break;
            case Action.START_TIME:
                holder.actionname.setText("任务时间：");
                String format = mDatas.get(position).getStarttime().getTime().getHours()+":"
                        +mDatas.get(position).getStarttime().getTime().getMinutes();
                holder.tvAction.setText(format);
                holder.etedit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        resetActiontime(position);

                    }
                });
                break;
            case Action.OPENAPP:
                holder.actionname.setText("打开app：");
                holder.tvAction.setText(mDatas.get(position).getOpenappname());
                break;
            case Action.DELAY:
                holder.actionname.setText("延迟：");
                holder.tvAction.setText(mDatas.get(position).getDelaytime()+"");
                break;
            case Action.CLICK:

                holder.actionname.setText("点击：");
                holder.tvAction.setText(mDatas.get(position).getClicktext());
                break;
            case Action.SLIDE:

                holder.actionname.setText("滑动：");
                holder.tvAction.setText(mDatas.get(position).getSlidestartend());
                break;
        }

        holder.btdele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatas.remove(position);
                notifyDataSetChanged();
            }
        });





    }


    public void resetActiontime(final int mposition){


        //添加任务时间弹窗
        CustomDialog.show(mActivity, R.layout.ly_dialogtimepick, new CustomDialog.BindView() {
            @Override
            public void onBind(final CustomDialog dialog, final View rootView) {
                //绑定布局


                final Button confirm=rootView.findViewById(R.id.bt_timepickconfirm);
                Button cancel=rootView.findViewById(R.id.bt_timepickcancel);
//                    ImageView btnOk = rootView.findViewById(R.id.btn_ok);
//                    //绑定事件
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Example：让对话框消失

                        TimePicker timepick=rootView.findViewById(R.id.timePicker);
                        timepick.getHour();
                        timepick.getMinute();
                        JfLog.logger(timepick.getHour()+":"+timepick.getMinute());

                        Calendar actioncalendar1 = Calendar.getInstance();
                        int hourofday = Integer.parseInt(timepick.getHour() + "");
                        int minute = Integer.parseInt(timepick.getMinute() + "");
                        actioncalendar1.set(1990, 12, 1, hourofday, minute);
                        mDatas.get(mposition).setStarttime(actioncalendar1);
                        dialog.doDismiss();
                        notifyDataSetChanged();
                    }
                });

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Example：让对话框消失
                        // dialog.doDismiss();
                        dialog.doDismiss();
                    }
                });

            }
        });

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @NonNull
    @Override
    public ActionListAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, parent, false);
        LyActionlistBinding inflate = LyActionlistBinding.inflate(LayoutInflater.from(mActivity),parent,false);
        VH vh = new VH(inflate);
        return vh;
    }
}